package com.zmn.oms.third.dj58.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Job;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.enums.ChannelFacade;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.core.tracker.FacadeTracker;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.dj58.service.DaoJia58Service;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderNewNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 类描述：向58到家直约推送订单变化通知的服务
 *
 * @author XieWenBing
 * @date 2019/11/05 14:20
 */

@Slf4j
@Service("daoJia58NotifyService")
public class DaoJia58NotifyServiceImpl implements OrderChangeNotifyService, OrderNewNotifyService {

    @Autowired
    protected DaoJia58Service daoJia58Service;

    @Resource
    TaskManager taskManager;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!DaoJia58Consts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", DaoJia58Consts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", DaoJia58Consts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        FacadeTracker.FacadeTrackerCore tracker = FacadeTracker.getTracker()
                .dataOut(ChannelFacade.DAOJIA_58)
                .orderId(orderWorkLogChange.getOrderId());

        try{
            switch (logType) {
                // 修改/确认
                // 针对首次同步接单失败的情况，可通过“修改”操作，再次触发同步接单状态
                case OrderLogConsts.ORDER_LOG_TYPE_INPUT:
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                    daoJia58Service.sendAcceptOrderTo58DaoJia(orderWorkLogChange);
                    break;
                // 派单
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    daoJia58Service.sendUpdateOrderTo58DaoJia(orderWorkLogChange);
                    break;
                // 工程师上门
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    daoJia58Service.sendBeginServiceTo58DaoJia(orderWorkLogChange);
                    break;
                // 取消、失败、弃单
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    daoJia58Service.sendCancelOrderTo58DaoJia(orderWorkLogChange);
                    break;
                // 完成
                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    daoJia58Service.sendFinishOrderTo58DaoJia(orderWorkLogChange);
                    break;
                default:
                    tracker.discard();
                    return;
            }
        } catch (OmsBaseException e) {
            log.info("【{}】<订单状态同步>失败，订单状态为:[{}]，提示消息:[{}]。", DaoJia58Consts.CHANNEL_NAME, logType,e.getMessage());
            tracker.error(e, e.getMessage());
        }
        log.info("【{}】<订单状态同步完成> 。", DaoJia58Consts.CHANNEL_NAME);
        tracker.commit();
    }

    @Override
    public void notifyNewOrder(OrderWorkLogChange orderWorkLogChange) throws OmsBaseException {
        // 判断功能是否开启
        if (!DaoJia58Consts.POWER_ON) {
            log.info("【{}】<订单状态同步-新订单> 该功能没有开启。", DaoJia58Consts.CHANNEL_NAME);
            return;
        }

        // 同步接单消息给58到家直约平台
        FacadeTracker.FacadeTrackerCore tracker = FacadeTracker.getTracker()
                .dataOut(ChannelFacade.DAOJIA_58)
                .orderId(orderWorkLogChange.getOrderId());

        try {

            log.info("【{}】<订单状态同步-新订单>,订单状态:[{}] ",DaoJia58Consts.CHANNEL_NAME,orderWorkLogChange.toString());
            daoJia58Service.sendAcceptOrderTo58DaoJia(orderWorkLogChange);
            tracker.commit();
            return;

        } catch (OmsBaseException e) {
            log.info("【{}】<订单状态同步-新订单>失败:[{}]。", DaoJia58Consts.CHANNEL_NAME,e.getMessage());
            tracker.error(e, e.getMessage());
        } catch (Exception e) {
            log.info("【{}】<订单状态同步-新订单>失败:[{}]。", DaoJia58Consts.CHANNEL_NAME,e.getMessage());
            tracker.error(e, e.getMessage());
        }
        tracker.commit();

        // 设置定时任务，5秒后重新同步一次
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_58DJ_SYNC_ACCEPT_ORDER_JOB + ":" + orderWorkLogChange.getOrderId());
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_58DJ_SYNC_ACCEPT_ORDER_JOB);
        job.setParam("orderWorkLogChange", JSON.toJSONString(orderWorkLogChange));
        job.setRepeatCount(1);
        Date dateRun = DateUtil.addSecond(DateUtil.getNow(), 5);
        job.setTriggerDate(dateRun);

        taskManager.submitTask2(job);
    }
}
